

#include "../include/bit.h"

int bit_get(const unsigned char *bits, int pos)
{
	unsigned char mask;

	mask = 0x80 >> (pos % 8);

	return (mask & bits[(int)pos/8] ? 1 : 0);
}

void bit_set(unsigned char *bits, int pos, int stat)
{
	unsigned char mask;
	mask = 0x80 >> (pos % 8);

	if (stat)
		bits[(int)pos / 8] = bits[(int) pos / 8] | mask;
	else
		bits[(int)pos / 8] = bits[(int) pos / 8] & (~mask);
}


void bit_xor(const unsigned char *bits1, const unsigned char *bits2, unsigned char *bitsx, int size)
{
	int idx;

	for (idx = 0; idx < size; idx++)
	{
		if (bit_get(bits1, idx) == bit_get(bits2, idx))
			bit_set(bitsx, idx, 0);
		else
			bit_set(bitsx, idx, 1);
	}

	return;
}


void bit_rot_left(unsigned char *bits, int size, int count)
{
	int loff = 0;
	int lbit = 0;
	int i,j;

	j = 0;
	while (j < count)
	{
		for (i = 0; i <= (int)(size - 1)/8; i++)
		{
			lbit = bit_get(&bits[i], 0);

			if (i == 0)
				loff = lbit;
			else
				bit_set(&bits[i - 1], 7, lbit);

			bits[i] = bits[i] << 1;
		}

		bit_set(bits, size - 1, loff);
		j++;
	}

	return;
}


